package com.young.config.service.impl;

import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.config.model.ConfigKv;
import com.young.config.service.IConfigKvService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 统一配置信息服务实现
* Created by wangjiyu on 2021-6-20.
*/
@Service("configKvService")
public class ConfigKvServiceImpl implements IConfigKvService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(ConfigKvServiceImpl.class);

    private static final String MAPPER = "com.young.config.mapper.configKv";//mapper的namespace

    //获取数据列表
    @Override
    public List<ConfigKv> getList(ConfigKv obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<ConfigKv> getPage(ConfigKv obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public ConfigKv get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    /**
     * 根据配置key查询配置
     *
     * @param key
     * @return
     */
    @Override
    public ConfigKv getByKey(String key) {
        return dataAccessService.getObject(MAPPER + ".getByKey", key);
    }

    //统计数量
    @Override
    public int count(ConfigKv obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(ConfigKv obj) {
        obj.setUpdateTime(new Date());
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(ConfigKv obj) {
        obj.setCreateTime(new Date());
        obj.setUpdateTime(obj.getCreateTime());
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<ConfigKv> list) {
        int i = 0;
        for(ConfigKv item : list){
            i += this.insert(item);
        }
        return i;
    }

}